<template>
  <!-- Banner & Image 通用组件 -->
  <div class="content">
    <p class="desc">添加Tab栏 (最多{{ pictureMaxNum }}个，可拖动排序）</p>
    <template>
      <ul>
        <vuedraggable
          v-model="list.content"
          tag="ul"
          draggable="li"
          v-if="list.content && list.content.length > 0"
          class="image-list"
        >
          <li v-for="(item, index) in list.content" :key="index">
            <div @click="removeNav(index)" class="el-icon-close"></div>
            <div style="width: calc(100% - 1.5rem)">
              <!-- 标题 -->
              <el-input
                v-model="item.label"
                placeholder="请输入内容"
                v-show="item.ifInputing"
                @focus="item.ifInputing = true"
                @blur="item.ifInputing = false"
                maxlength="4"
              ></el-input>
              <div v-show="!item.ifInputing" @click="item.ifInputing = true">
                {{ item.label }}
              </div>
            </div>
            <div class="img-box" @click="addImage(index, 'images')">
              <img v-if="item.images" :src="item.images" alt="" />
              <img v-else src="~@/assets/images/pictures/chanpin2.png" alt="" />
            </div>
            <div
              class="img-box"
              @click="addImage(index, 'activeImages')"
              style="margin-right: 0.2rem"
            >
              <img v-if="item.activeImages" :src="item.activeImages" alt="" />
              <img v-else src="~@/assets/images/pictures/chanpin1.png" alt="" />
            </div>
          </li>
        </vuedraggable>
      </ul>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="addNav"
        class="add-image"
      >
        添加导航
      </el-button>
    </template>
    <el-upload
      ref="upload"
      :http-request="httpRequest"
      :show-file-list="false"
      multiple
      action
      style="display: none"
    >
    </el-upload>
  </div>
</template>

<script>
import vuedraggable from 'vuedraggable'
import DlUpload from '@/components/common/DlUpload'
export default {
  name: 'Images',
  components: {
    vuedraggable,
    DlUpload,
  },
  props: {
    comData: {
      // 这个模块的对象数据
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      type: 'Navigation',
      pictureMaxNum: 5,
      pictureMinNum: 2,
      list: {},
      // 编辑图片
      imageIndex: '',
      imageType: '',
      //   图片
      fileTypes: ['JPEG', 'JPG', 'PNG'],
      image: [],
      picture: [],
      files: {},
    }
  },
  computed: {
    size() {
      return this.list.type == 'Images' ? '750*750' : '750*400'
    },
    len() {
      return this.list.type == 'Images' ? 1 : 10
    },
  },
  mounted() {
    this.list = this.comData
  },
  methods: {
    // 添加导航
    addNav() {
      if (this.list.content.length == this.pictureMaxNum) {
        this.$message.error('最多添加' + this.pictureMaxNum + '个')
        return false
      }
      this.list.content.push({
        label: '点击修改',
        images: '',
        activeImages: '',
        ifInputing: false,
      })
    },
    // 删除导航
    removeNav(index) {
      if (this.list.content.length <= this.pictureMinNum) {
        this.$message.error('至少添加' + this.pictureMinNum + '个')
        return false
      }
      this.list.content.splice(index, 1)
    },
    // 重新上传图片
    addImage(index, imageType) {
      this.imageIndex = index
      this.imageType = imageType
      this.$refs['upload'].$children[0].$refs.input.click()
    },
    // 上传照片
    async httpRequest(content) {
      try {
        const formData = new FormData()
        formData.append('file', content.file)
        const res = await this.$http.post(
          this.$API.baseApi.uploadImage,
          formData
        )
        if (res.data.code === 200 && res.data.success) {
          this.list.content[this.imageIndex][this.imageType] = res.data.result
        }
      } catch (e) {
        this.$message.error(e)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
li {
  margin: 0.2rem;
  margin-bottom: 10px;
  padding: 20px 14px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  border-radius: 4px;
  background-color: #f7f8f9;
  cursor: pointer;
  list-style: none;
  .el-icon-close {
    position: absolute;
    right: 0.1rem;
    top: 0.1rem;
  }
  .img-box {
    img {
      width: 0.6rem;
      height: 0.6rem;
    }
  }
}

.content {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  height: 100%;
  overflow: hidden;
  .desc {
    text-align: center;
    font-size: 12px;
    color: #666;
    margin: 18px 0;
    padding-bottom: 10px;
    border-bottom: 1px dashed #ddd;
  }
  .size {
    text-align: center;
    font-size: 12px;
    color: #999;
    margin-bottom: 0;
  }
  .add-image {
    width: calc(100% - 20px);
    height: 34px;
    line-height: 34px;
    padding: 0;
    font-size: 12px;
    margin-left: 10px;
    margin-top: 10px;
  }
}
</style>